package com.kittlen.managementSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kittlen.managementSystem.entity.Coach;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Vehicle;
import com.kittlen.managementSystem.mapper.CoachMapper;
import com.kittlen.managementSystem.service.ICoachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kittlen.managementSystem.service.IVehicleService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 教练表 服务实现类
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements ICoachService {

    @Resource
    IVehicleService iVehicleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAndVehicle(Coach coach, String vehicleId,String vehicleOldId) throws Exception {
        try {
            if(ObjectUtil.isNoEmpty(vehicleId)){
                iVehicleService.update(new UpdateWrapper<Vehicle>().eq("vehicle_id",vehicleId).set("coach_id",coach.getCoachId()));
                if(ObjectUtil.isNoEmpty(vehicleOldId)){
                    iVehicleService.update(new UpdateWrapper<Vehicle>().eq("vehicle_id",vehicleOldId).set("coach_id",null));
                }
            }
            this.updateById(coach);
        }catch (Exception e){
            throw e;
        }
        return true;
    }
}
